local more1__fuli = fk.CreateSkill {
  name = "more1__fuli",
}

Fk:loadTranslationTable{
  ["more1__fuli"] = "伏枥",
  [":more1__fuli"] = "每轮限一次，当你处于濒死状态时，你可以减一点体力上限并弃置“当先”记录过的【杀】，"..
  "再将体力回复至体力上限，然后随机抽取八张武将牌并获得其中X个技能。"..
  "最后若你的体力值为全场最多，你翻面。(X为你上次当先弃置【杀】的数量，至少为1)",
  ["#more1__fuli-choice"] = "选择至少一个技能",
  ["$more1__fuli1"] = "老将之躯，燃已为国!",
  ["$more1__fuli2"] = "贼将未除，老夫岂能轻易倒下。",
}

more1__fuli:addEffect(fk.AskForPeaches, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(more1__fuli.name) and player.dying and
      player:usedSkillTimes(more1__fuli.name, Player.HistoryRound) == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:changeMaxHp(player,-1,more1__fuli.name)
    room:recover { num = player.maxHp-player.hp, skillName = more1__fuli.name, who = player, recoverBy = player}

    local generals = room.general_pile
    if #generals > 0 then
      generals = table.random(generals, 8)

      local skills = {}
      for _, general_name in ipairs(generals) do
        local general = Fk.generals[general_name]
        local general_skills = {}
        for _, s in ipairs(general:getSkillNameList()) do
          local skill = Fk.skills[s]
          if not (skill:hasTag(Skill.AttachedKingdom) and not table.contains(skill:getSkeleton().attached_kingdom, player.kingdom)) then
            table.insertIfNeed(general_skills, s)
          end
        end
        table.insertIfNeed(skills, general_skills)
      end
      if #skills > 0 then
        local result = player.room:askToCustomDialog(player, {
          skill_name = more1__fuli.name,
          qml_path = "packages/tenyear/qml/ChooseGeneralSkillsBox.qml",
          extra_data = {generals, skills, 1, math.max(1,player:getMark("@more1__dangxian")), "#more1__fuli-choice", false}
        })
        local choices = result ~= "" and result or table.random(skills, 2)
        room:handleAddLoseSkills(player, table.concat(choices, "|"))
      end
    end
    if player:getMark("@more1__dangxian") > 0 then
      room:setPlayerMark(player,"@more1__dangxian", 0)
    end

  end,
})

return more1__fuli
